/////////////////////////////////////////////////////////////////////////////
// Original code from libhdfs3. Copyright (c) 2013 - 2014, Pivotal Inc.
// All rights reserved. Author: Zhanwei Wang
/////////////////////////////////////////////////////////////////////////////
//  Modifications by Kumo Inc.
// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//



#ifndef _HDFS_LIBHDFS3_CLIENT_LOCALBLOCKREADER_H_
#define _HDFS_LIBHDFS3_CLIENT_LOCALBLOCKREADER_H_

#include <kmhdfs/client/block_reader.h>
#include <kmhdfs/common/checksum.h>
#include <kmhdfs/common/file_wrapper.h>
#include <memory>
#include <kmhdfs/client/read_short_circuit_info.h>
#include <kmhdfs/common/session_config.h>

#include <vector>

namespace Hdfs {
namespace Internal {

class LocalBlockReader: public BlockReader {
public:
    LocalBlockReader(const std::shared_ptr<ReadShortCircuitInfo>& info,
                     const ExtendedBlock & block, int64_t offset, bool verify,
                     SessionConfig & conf, std::vector<char> & buffer);

    ~LocalBlockReader();

    /**
     * Get how many bytes can be read without blocking.
     * @return The number of bytes can be read without blocking.
     */
    virtual int64_t available() {
        return length - cursor;
    }

    /**
     * To read data from block.
     * @param buf the buffer used to filled.
     * @param size the number of bytes to be read.
     * @return return the number of bytes filled in the buffer,
     *  it may less than size. Return 0 if reach the end of block.
     */
    virtual int32_t read(char * buf, int32_t size);

    /**
     * Move the cursor forward len bytes.
     * @param len The number of bytes to skip.
     */
    virtual void skip(int64_t len);

private:
    /**
     * Fill buffer and verify checksum.
     * @param bufferSize The size of buffer.
     */
    void readAndVerify(int32_t bufferSize);
    int32_t readInternal(char * buf, int32_t len);

private:
    bool verify; //verify checksum or not.
    const char * pbuffer;
    const char * pMetaBuffer;
    const ExtendedBlock & block;
    int checksumSize;
    int chunkSize;
    int localBufferSize;
    int position; //point in buffer.
    int size;  //data size in buffer.
    int64_t cursor; //point in block.
    int64_t length; //data size of block.
    std::shared_ptr<Checksum> checksum;
    std::shared_ptr<FileWrapper> dataFd;
    std::shared_ptr<FileWrapper> metaFd;
    std::shared_ptr<ReadShortCircuitInfo> info;
    std::vector<char> & buffer;
    std::vector<char> metaBuffer;
};

}
}

#endif /* _HDFS_LIBHDFS3_CLIENT_LOCALBLOCKREADER_H_ */
